﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace SRF.Data.Impl
{
    /// <summary>
    /// database 工厂
    /// </summary>
    public class DatabaseFactory
    {
        static IDictionary<string, Database> databases;

        static DatabaseFactory()
        {
            databases = new Dictionary<string, Database>();
        }

        public static Database Get(DatabaseType dbtype, string connectionstring)
        {
            return Get(dbtype.ToString(), connectionstring);
        }

        public static Database Get(string dbtype, string connectionstring)
        {
            dbtype = dbtype.ToLower();
            switch (dbtype)
            {
                case "mssql":
                    return new SqlDatabase(connectionstring);
                case "mysql":
                    return new MySqlDatabase(connectionstring);
                //case "sqlite":
                //    return new SqliteDatabase(connectionstring);
                default:
                    if (databases.ContainsKey(dbtype))
                        return databases[dbtype];
                    break;
            }

            throw new NotImplementedException("Database 未实现");
        }

        /// <summary>
        /// 注册database
        /// </summary>
        /// <param name="key"></param>
        /// <param name="database"></param>
        public static void Register(string key, Database database)
        {
            key = key.ToLower();
            if (databases.ContainsKey(key))
            {
                databases[key] = database;
            }
            else
            {
                databases.Add(key, database);
            }
        }
    }
}